﻿using MediatR;
using Ordering.Domain.Aggregate;

namespace Ordering.Application.Commands
{
    public class UpdateBalanceBuyerCommand : IRequest
    {
        public string Id { get; set; }
        public decimal Amount { get; set; }

        public UpdateBalanceBuyerCommand(string id, decimal amount)
        {
            Id = id;
            Amount = amount;
        }
    }

    public class UpdateBalanceBuyerCommandHandler
        : IRequestHandler<UpdateBalanceBuyerCommand>
    {
        private readonly IBuyerStogage _orderStogage;

        public UpdateBalanceBuyerCommandHandler(IBuyerStogage orderStogage)
        {
            _orderStogage = orderStogage;
        }

        public async Task Handle(UpdateBalanceBuyerCommand request, CancellationToken cancellationToken)
        {
             var buyer = await _orderStogage.GetByIdAsync(request.Id);
              buyer.UpdateBalace(request.Amount);
             await _orderStogage.UnitOfWork.SaveEntitiesAsync(cancellationToken);
        }
    }
}
